使用字符串构建上传文件内容
function Coroutine\Http\Client->addData(string $data, string $name, string $mimeType = null,
string $filename = null)
$data
数据内容,必选参数,最大长度不得超过buffer_output_size
$name
表单的名称,必选参数,$_FILES
参数中的key
$mimeType
文件的MIME
格式,可选参数,默认为application/octet-stream
$filename
文件名称,可选参数,默认为$name
使用addData
会自动将POST
的Content-Type
将变更为form-data
。
addData
在4.1.0
以上版本可用
$cli = new Swoole\Coroutine\Http\Client('httpbin.org', 80);
$cli->setHeaders([
'Host' => "httpbin.org"
]);
$cli->set(['timeout' => -1]);
$cli->addData(Co::readFile(__FILE__), 'file1', 'text/plain');
$cli->post('/post', ['foo' => 'bar']);
echo $cli->body;
$cli->close();